var $option_template = $("#providerList option:eq(0)");
	var $select = $("#providerList");
$(function(){
	//加载分页
	loadData_page();
	
	//加载供应商列表-条件
	loadData_provider();
});

var params;//请求的条件
//加载分页-数据行数
function loadData_page(){
	//请求数据行数
	params = {
		"productName":$("#productName").val(),
		"providerId":$("#providerList").val(),
		"isPayment":$("#isPayment").val()
	};
	requestAjax("/SSM/view/bill/getBillListPageCount.htm","get","json",params,
		function(data){
			if(data.success){
				//渲染分页
				render_page(data.result);
			}else{
				layer.msg(data.message);
			}
		});
}

//制作分页条
function render_page(count){
	layui.use("laypage",function(){
		var laypage = layui.laypage;
		
		laypage.render({
			elem:"page",
			theme:"#4287c2",
			limit:6,
			count:count,
			layout:['prev', 'page', 'next', 'count', 'skip'],
			jump: function(obj, first){
			    //加载订单信息列表
			    loadData_bill(obj.curr, obj.limit);
			}
		});
	});
}

//加载订单信息列表
function loadData_bill(pageNum, pageSize){
	params.pageNum = pageNum;
	params.pageSize = pageSize;

	requestAjax("/SSM/view/bill/getBillListPage.htm","get","json",params,
		function(data){
			if(data.success){
				//渲染订单列表
				render_bill(data.result);
			}else{
				layer.msg(data.message);
			}	
		});
}

//渲染订单列表
var $bill_table = $("#bill_table");
var $tr_template = $bill_table.find("tr:eq(1)"); //假数据作为模板
function render_bill(data){
	$bill_table.find("tr:gt(0)").remove();
	for ( var i in data) {
		var $tr = $tr_template.clone();
		$tr.find("#billCode").text(data[i].billCode);
		$tr.find("#productName").text(data[i].productName);
		$tr.find("#providerName").text(data[i].providerName);
		$tr.find("#totalPrice").text(data[i].totalPrice);
		$tr.find("#isPayment").text(data[i].isPayment == 1 ? "未付款" : "已付款");
		$tr.find("#creationDate").text(data[i].creationDate);
		$tr.find("#bill_update").attr("href","javascript:bill_update("+data[i].id+")");
		$tr.find("#bill_delete").attr("href","javascript:bill_delete("+data[i].id+")"); 
		$bill_table.append($tr);
	}  
}


//删除
function bill_delete(billid){ 
 	layer.confirm('您是否要删除？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
        requestAjax("/SSM/view/bill/deleteBill.htm","get","json",{"id":billid},function(data){ 
        	if(data.success){
        		layer.msg("删除订单成功");
        		loadData_page();//删除成功后,重新分页
        	}else{
        		layer.msg(data.msg);
        	} 
        })  
		}, function(){
		  layer.msg('也可以这样', {
		    time: 20000, //20s后自动关闭
		    btn: ['明白了', '知道了']
		  });
		});  
}


//修改
function bill_update(billId){
	 //加载页面 
	$("#content").load("../view/bill_add_upd.html",function(){
		setId(billId);
	});
}

function open(){
	$("#content").load("../view/bill_add_upd.html");
    
	
}
 




 